package com.gxt.sysmanage.controller;

import com.gxt.sysmanage.service.IConfigService;
import com.gxt.sysmanage.domain.Config;
import com.gxt.sysmanage.query.ConfigQuery;
import com.gxt.basic.utils.AjaxResult;
import com.gxt.basic.utils.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/config")
@Api(value = "系统配置controller",description = "系统配置controller")
public class ConfigController {
    @Autowired
    public IConfigService configService;



    @PutMapping
    @ApiOperation(value = "保存系统配置")
    public AjaxResult save(@RequestBody Config config){
        try {
            if( config.getId()!=null)
                configService.update(config);
            else
                configService.insert(config);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }


    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "删除系统配置")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            configService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "查询配置详情")
    public AjaxResult loadById(@PathVariable("id")Long id)
    {
        try {
            Config config = configService.loadById(id);
            return AjaxResult.me().setResult(config);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    @GetMapping
    @ApiOperation(value = "查询所有配置")
    public AjaxResult getAll(){

        try {
            List< Config> list = configService.loadAll();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }



    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody ConfigQuery query)
    {
        try {
            PageList<Config> pageList = configService.pageList(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    //批量删除
    @PatchMapping
    @ApiOperation(value = "批量删除部门")
    public AjaxResult batchDelete(@ApiParam(required = true) @RequestBody List<Long> ids){
        try {
            configService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
        return AjaxResult.me();
    }

}
